package com.xr.core.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xr.core.system.common.annotation.OperationLogAnno;
import com.xr.core.system.common.base.MyPage;
import com.xr.core.system.common.base.Result;
import com.xr.core.system.common.enums.OperationType;
import com.xr.core.system.common.groups.Save;
import com.xr.core.system.common.groups.Update;
import com.xr.core.system.entity.roleModule.AssignmentPermissionDTO;
import com.xr.core.system.entity.roleModule.Role;
import com.xr.core.system.service.RoleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 角色表(Role)表控制层
 */
@Validated
@RestController
@RequestMapping("/api/v1/role")
public class RoleController {

    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    /**
     * 给角色分配权限
     *
     * @param assignmentPermissionDTO
     * @return
     */
    @PostMapping("/assignmentPermissionForRole")
    @OperationLogAnno(module = "角色表模块", type = OperationType.INSERT, desc = "给角色分配权限")
    public Result<Object> assignmentPermissionForRole(@RequestBody AssignmentPermissionDTO assignmentPermissionDTO) {
        return Result.isSuccess(roleService.assignmentPermissionForRole(assignmentPermissionDTO));
    }

    /**
     * 停启用角色
     *
     * @param roleId
     * @return
     */
    @PutMapping("/disOrEnaAccount")
    @OperationLogAnno(module = "角色表模块", type = OperationType.UPDATE, desc = "停启用角色")
    public Result<Object> disOrEnaAccount(@Validated @NotNull(message = "账号id不能为空") Long roleId) {
        Role role = new Role();
        LambdaUpdateWrapper<Role> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(Role::getInitial, false).setSql("enable = not enable").eq(Role::getId, roleId);
        return Result.isSuccess(roleService.update(role, updateWrapper));
    }

    /**
     * 获取当前角色已选的权限
     *
     * @param id 角色id
     * @return
     */
    @GetMapping("/listCheckedPermissionForRole")
    public Result<List<Long>> listCheckedPermissionForRole(@Validated @NotNull(message = "角色id不能为空") Long id) {
        return Result.success(roleService.listCheckedPermissionForRole(id));
    }

    /**
     * 获取用户关联的角色
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/roleByUserId")
    Result<List<Long>> getRoleByUserId(Long id) {
        return Result.success(roleService.getRoleByUserId(id));
    }

    /**
     * 批量设置内置角色
     *
     * @param idList
     * @return
     */
    @PutMapping("/setInitRole")
    @OperationLogAnno(module = "角色表模块", type = OperationType.UPDATE, desc = "批量设置内置角色")
    public Result<Object> setInitRole(@RequestBody List<Long> idList) {
        return Result.isSuccess(roleService.setInitRole(idList));
    }

    /**
     * 批量设置默认角色
     *
     * @param idList
     * @return
     */
    @PutMapping("/setDefaultRole")
    @OperationLogAnno(module = "角色表模块", type = OperationType.UPDATE, desc = "批量设置默认角色")
    public Result<Object> setDefaultRole(@RequestBody List<Long> idList) {
        return Result.isSuccess(roleService.setDefaultRole(idList));

    }

    /**
     * 获取全部内置角色id
     *
     * @return
     */
    @GetMapping("/listInitRole")
    public Result<List<Object>> listInitRole() {
        LambdaQueryWrapper<Role> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Role::getInitial, true).eq(Role::getEnable, true);
        queryWrapper.select(Role::getId);
        return Result.success(roleService.listObjs(queryWrapper));
    }

    /**
     * 获取全部默认角色id
     *
     * @return
     */
    @GetMapping("/listDefaultRole")
    public Result<List<Object>> listDefaultRole() {
        return Result.success(roleService.listDefaultRole());
    }

    /**
     * 查询所有数据
     *
     * @param role 查询实体
     * @return 所有数据
     */
    @GetMapping("/queryAll")
    public Result<List<Role>> queryAll(Role role) {
        role.setEnable(true);
        return Result.success(roleService.list(new QueryWrapper<>(role)));
    }

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param role 查询实体
     * @return 所有数据
     */
    @GetMapping("/queryByPage")
    public Result<MyPage<Role>> queryByPage(MyPage<Role> page, Role role) {
        return Result.success(roleService.page(page, new QueryWrapper<>(role)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryOne/{id}")
    public Result<Role> selectOne(@PathVariable Serializable id) {
        return Result.success(roleService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param role 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @OperationLogAnno(module = "角色模块", type = OperationType.INSERT, desc = "新增数据")
    public Result<Object> insert(@Validated(Save.class) @RequestBody Role role) {
        return Result.isSuccess(roleService.insert(role));
    }

    /**
     * 修改数据
     *
     * @param role 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @OperationLogAnno(module = "角色模块", type = OperationType.UPDATE, desc = "修改数据")
    public Result<Object> update(@Validated(Update.class) @RequestBody Role role) {
        return Result.isSuccess(roleService.update(role));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    @OperationLogAnno(module = "角色模块", type = OperationType.DELETE, desc = "删除单条数据")
    public Result<Object> delete(@PathVariable Long id) {
        Role role = new Role();
        LambdaUpdateWrapper<Role> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(Role::getId, id).eq(Role::getInitial, false);
        return Result.isSuccess(roleService.deleteWithFill(role, updateWrapper));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/batchDelete")
    @OperationLogAnno(module = "角色模块", type = OperationType.DELETE, desc = "批量删除数据")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        Role role = new Role();
        role.setInitial(false);
        return Result.isSuccess(roleService.batchDelete(idList, role));
    }

}

